package pos.gui;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import pos.controller.UtenteController;
import pos.domain.Piano;
import pos.domain.TipoLinkU_P;
import pos.domain.Utente;
import pos.server.dac.exceptions.EccezioneCRUD;

public class Index implements EntryPoint
{
    private VerticalPanel rootPanel = new VerticalPanel();
    private HorizontalPanel matricolaPanel = new HorizontalPanel();
    private TextBox matricolaBox = new TextBox();
    private HorizontalPanel passwordPanel = new HorizontalPanel();
    private PasswordTextBox passwordBox = new PasswordTextBox();
    private Button loginButton = new Button("Login");
    private HorizontalPanel menu = new HorizontalPanel();
    private static Utente utente = null;
    private PianiView pianiView;
    private OperazioneFinanziariaView ofView;
    private boolean toRefresh = true;

    @Override
    public void onModuleLoad()
    {
        if (!GWT.isScript()) {
            matricolaBox.setText("1342948");
            passwordBox.setText("123");
        }
        rootPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        matricolaPanel.add(new Label("Matricola: "));
        matricolaPanel.add(matricolaBox);
        passwordPanel.add(new Label("Password: "));
        passwordPanel.add(passwordBox);
        rootPanel.add(matricolaPanel);
        rootPanel.add(passwordPanel);
        UtenteController.setView(this);
        System.out.println("Adding click handler");
        loginButton.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                UtenteController.login(matricolaBox.getText(), passwordBox.getText());
                System.out.println("onClick fired");
            }
        });
        rootPanel.add(loginButton);
        rootPanel.setStyleName("centrato");
        RootPanel.get().add(rootPanel);
        repaint();
    }

    public static Utente getUtente()
    {
        return utente;
    }

    private static void setUtente(Utente u)
    {
        utente = u;
    }

    public void logged(Utente u)
    {
        setUtente(u);
        rootPanel.clear();
        rootPanel.removeStyleName("centrato");
        RootPanel.getBodyElement().getStyle().setProperty("backgroundImage", "url(images/bg2.png)");
        rootPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        menu.clear();
        Label pianoDiStudioMenu = new Label("Piani di studio");
        pianoDiStudioMenu.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                pianiView.setVisible(true);
                ofView.setVisible(false);
                System.out.println(pianiView.getModel());
            }
        });
        Label operazioniFinanziarieMenu = new Label(" Gestione Finanze");
        operazioniFinanziarieMenu.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                pianiView.setVisible(false);
                ofView.setVisible(true);
            }
        });
        //Label attivitaMenu = new Label("Calendario");
        pianoDiStudioMenu.setStyleName("menu");
        operazioniFinanziarieMenu.setStyleName("menu");
        menu.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        menu.add(pianoDiStudioMenu);
        menu.add(operazioniFinanziarieMenu);
        menu.setStyleName("menuPanel");
        RootPanel.get().add(menu);
        //rootPanel.add(menu);
        pianiView = new PianiView();
        pianiView.setVisible(false);
        Set<TipoLinkU_P> linkCrea = u.getLinkCrea();
        Collection<Piano> col = new LinkedList<Piano>();
        for (TipoLinkU_P link : linkCrea)
            col.add(link.getPiano());
        pianiView.setModel(col);
        rootPanel.add(pianiView);

        ofView = new OperazioneFinanziariaView();
        ofView.setVisible(false);
        rootPanel.add(ofView);
        if (toRefresh) {
            toRefresh = false;
            repaint();
        }
    }

    private void repaint() {
        RootPanel.getBodyElement().getStyle().setProperty("display", "none");
        RootPanel.getBodyElement().getStyle().setProperty("display", "block");
    }
}
